unit SSBRecruitChartUnit;

interface


uses DiagramFactory, datacontainer, pganalysistypesunit, apgabstractunit, classes,
   xmlintf, pgseriesunit, chartaxisunit, ifmmemoryunit, math, sysutils;


type
   SSBRecruitChart = class(TasacsChart)
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; datacontainer : TasacsDataContainer); reintroduce;
   end; // SSBRecruitChart


   RecruitSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
   end; // RecruitSeries


   SSBAxis = class(PgChartAxis)
   protected
      function GetNumDimensionElms : Integer; override;
      function GetAxisLabel : string; override;
   end; // SSBAxis


   SSBAxisSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
      function GetIntervalName(I : Integer) : string; override;
   end; // SSBAxisSeries

implementation


//////////////////////////////////
// SSBRecruitChart


function SSBRecruitChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := SSBAxis.Create(Self as IChart, Axis);
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // SSBRecruitChart.CreateChartAxis


function SSBRecruitChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if Assigned(MetaSeries) then
           Result := RecruitSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := SSBAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
   end; // case
end; // SSBRecruitChart.CreateSeries


constructor SSBRecruitChart.Create(AOwner : TComponent; ItemNode : IXMLNode; Caption : string; datacontainer : TasacsDataContainer);
var
   M : APgMetaSeries;
begin
   inherited Create(AOwner, ItemNode, 'Chart', datacontainer);
   Self.Caption := Caption;
   Self.ShowCategories := true;
   Self.Scatterlegends := true;
   if not Assigned(ItemNode) then
   begin
      ShowSeriesLegend := false;
      ShowMeanLegend := false;
//      ChartAxis[atX].ShowIntervalSeries := true;
      PlotType := ptScatterSingle;

//      chartaxis[atX].ShowMajorGrid := true;
//      chartaxis[atX].ContinuousGroupData := true;
      chartaxis[atX].ShowMinorTick := false;
      chartaxis[atx].DefaultMin := false;
      chartaxis[atx].MinValue := 0;
      chartaxis[atx].ShowMajorGrid := true;
      chartaxis[aty].DefaultMin := false;
      chartaxis[aty].MinValue := 0;
      //chartaxis[atX].AxisLabelPosition := alpCenter;
      chartaxis[aty].AxisLabel := 'Recruits';
      chartaxis[aty].AxisLabelPosition := alpCenter;
      chartaxis[aty].DefaultAxisLabel := false;
      chartaxis[aty].ShowMajorGrid := true;
      // Setup meta series at y axis:
      with ChartAxis[atY].MetaSeries do
      begin
         M := TasacsMetaSeries.Create(ChartAxis[aty], nil, 'SSB and recruits', 0);
         Add(Pointer(M));
         M.Scatter := true;
      end;
   end;
end;


///////////////////////////////
// RecruitSeries


function RecruitSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   case SeriesVariable of
      svIntervalPointPos:
         with TasacsChart(PrimaryChartAxis.ChartComp).Datacontainer do
            Result := FloatArray(StdData[ord(stdRecruits)])[I + FirstAge];
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // RecruitSeries.GetInternalValue


////////////////
// SSBAxisSeries


function SSBAxisSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   Assert(IsAxisSeries);
   case SeriesVariable of
      svIntervalLow:
         with TasacsChart(PrimaryChartAxis.ChartComp).DataContainer do
            Result := FloatArray(StdData[ord(stdSSB)])[I + MinCategory];
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   end; // case
end; // SSBAxisSeries.GetInternalValue


function SSBAxisSeries.GetIntervalName(I : Integer) : string;
var
   Year : Integer;
begin
   // I range : [0..NumDimElms - 1]; // not relative to axis selection.
   with TasacsChart(PrimaryChartAxis.ChartComp).DataContainer do
      Year := I + FirstYear - FirstAge;
   Result := inttostr(Year);
end; // SSBAxisSeries.GetIntervalName


///////////////////
// SSBAxis


function SSBAxis.GetAxisLabel : string;
begin
   if DefaultAxisLabel then
      Result := 'SSB'
   else
      Result := inherited GetAxisLabel;
end; // SSBAxis.GetAxisLabel


function SSBAxis.GetNumDimensionElms : Integer;
begin
   with TasacsChart(ChartComp).DataContainer do
      Result := NumYears - FirstAge; //  = Number of SSB Rqcruit pairs
end; // SSBAxis.GetNumDimensionElms


end.
